package gameBasics;

import chainOfResponsibility.ColliderChain;
import frame.GameFrame;
import gameLevel.GameLevelControl;
import gameLevel.classicLevel.ClassicLevel_1;
import gameLevel.classicLevel.ClassicLevel_2;
import gameLevel.classicLevel.ClassicLevel_3;
import gameLevel.classicLevel.ClassicLevel_4;
import gameLevel.survivalLevel.SurvivalLevel;
import tank.Dir;
import tank.MainTank;
import tank.TankType;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 对外门面, 对内调停
 */
public class GameModel {

    private final MainTank mainTank = new MainTank(TankType.MAIN,300,550, Dir.UP);    //主坦克
    public ColliderChain colliderChain = new ColliderChain();           //碰撞责任链
    private List<GameObject> gameObjects = new ArrayList<>();           //游戏物品容器

    /**
     * 单例模式
     */
    private GameModel(){

    }

    private static class GameModelHolder{
        private final static GameModel INSTANCE = new GameModel();

        static {
            INSTANCE.initGameModel();
        }
    }

    public static GameModel getInstance(){
        return GameModel.GameModelHolder.INSTANCE;
    }

    public MainTank getMainTank() {
        return mainTank;
    }

    /**
     * 初始化GameModel
     */
    private void initGameModel(){
        switch (GameLevelControl.gameLevel)
        {
            case CLASSIC_LEVE1 -> new ClassicLevel_1();
            case CLASSIC_LEVE2 -> new ClassicLevel_2();
            case CLASSIC_LEVE3 -> new ClassicLevel_3();
            case CLASSIC_LEVE4 -> new ClassicLevel_4();
            case SURVIVAL -> new SurvivalLevel();
        }
    }

    /**
     * 添加游戏物品
     * @param gameObject 游戏物品对象
     */
    public void addGameObject(GameObject gameObject){
        this.gameObjects.add(gameObject);
    }

    /**
     * 移除游戏物品
     * @param gameObject 游戏物品对象
     */
    public void removeGameObject(GameObject gameObject){
        this.gameObjects.remove(gameObject);
    }

    /**
     * 移除全部游戏物品
     * 关卡切换时移除GameModel中的全部游戏物品并重新添加绘制
     */
    public void removeAllGameObject(){
        gameObjects = new ArrayList<>();
    }

    /**
     * 重绘游戏物品
     * 用于关卡的选择及切换
     */
    public void rePaint(){
        removeAllGameObject();
        initGameModel();
        GameLevelControl.chooseLevelState = true;
        //重置主坦克坐标
        mainTank.x = 300;
        mainTank.y = 550;
    }

    /**
     * 绘制游戏结束时显示的内容
     * @param graphics 画笔对象
     */
    private void gameOver(Graphics graphics){
        removeAllGameObject();
        Color color = graphics.getColor();
        graphics.setColor(Color.white);
        graphics.setFont(new Font("宋体", Font.PLAIN, 16));
        if(!GameLevelControl.passState){
            graphics.drawString("游戏失败! 请按Esc键回到主界面……", GameFrame.GAME_WIDTH - 240 >> 1,GameFrame.GAME_HEIGHT / 3 + 80);
        }else {
            graphics.drawString("恭喜你成功通关! 请按Esc键回到主界面……", GameFrame.GAME_WIDTH - 280 >> 1,GameFrame.GAME_HEIGHT / 3 + 80);
        }
        graphics.setColor(color);
    }

    /**
     * 绘制游戏物品
     * @param graphics 画笔对象
     */
    public void paint(Graphics graphics) {
        //关卡的选择
        if(!GameLevelControl.chooseLevelState) rePaint();

        //关卡的切换
        if(!GameLevelControl.gameOverState && GameLevelControl.passState){
            rePaint();
            GameLevelControl.passState = false;
        }

        //绘制主坦克
        mainTank.paint(graphics);

        //处理游戏结束
        if(GameLevelControl.gameOverState) gameOver(graphics);

        //绘制全部游戏物品
        //TODO: Modify this code
        for(int i = 0; i < gameObjects.size(); i++){
            gameObjects.get(i).paint(graphics);
        }

        //碰撞逻辑(互相碰撞)
        for(int i = 0; i < gameObjects.size(); i++){
            for(int j = i + 1; j < gameObjects.size(); j++){
                GameObject o1 = gameObjects.get(i);
                GameObject o2 = gameObjects.get(j);

                colliderChain.collide(o1, o2);
            }
        }
    }

}
